package com.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class FutureTaskTest {

    private static volatile  boolean isEnd = false;
    private static volatile  Double end = 100D;
    public static void main(String[] args) throws InterruptedException {
        Double[] steps = new Double[]{0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.10};
        CountDownLatch countDownLatch = new CountDownLatch(steps.length);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(steps.length);
        //Semaphore semaphore = new Semaphore(steps.length);
        List<FutureTask> futureTaskList = new ArrayList<FutureTask>();
        for(int i=0;i<steps.length;i++){
            Double step = steps[i];
            FutureTask<Double> ft = new FutureTask<Double>(new Callable<Double>() {
                @Override
                public Double call() throws Exception {
                    cyclicBarrier.await();
                    //semaphore.acquire();
                    //countDownLatch.countDown();
                    Double point = 0D;
                    while(!isEnd){
                        if(point>=end){
                            isEnd = true;
                        }else{
                            point += step;
                        }
                    }
                    //semaphore.release();
                    return point;
                }
            });
            futureTaskList.add(ft);
        }
        for(int i=0;i<futureTaskList.size();i++){
            FutureTask ft = futureTaskList.get(i);
            new Thread(ft).start();
        }
        for(int i=0;i<futureTaskList.size();i++){
            FutureTask ft = futureTaskList.get(i);
            try {
                System.out.println(ft.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        /*
        FutureTask<Double> ft1 = new FutureTask<Double>(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                Double point = 0D;
                Double step = 0.1D;
                while(!isEnd){
                    if(point>=100){
                        isEnd = true;
                    }else{
                        point += step;
                    }
                }
                return point;
            }
        });
        FutureTask<Double> ft2 = new FutureTask<Double>(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                Double point = 0D;
                Double step = 0.2D;
                while(!isEnd){
                    if(point>=100){
                        isEnd = true;
                    }else{
                        point += step;
                    }
                }
                return point;
            }
        });
        FutureTask<Double> ft3 = new FutureTask<Double>(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                Double point = 0D;
                Double step = 0.3D;
                while(!isEnd){
                    if(point>=100){
                        isEnd = true;
                    }else{
                        point += step;
                    }
                }
                return point;
            }
        });*/

    }



}
